package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Information;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.SearchQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IInformationService;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;

    @Autowired
    private IInformationService informationService;

    @Override
    public void save(Travel travel) {
        //添加游记基本信息
        travelMapper.insert(travel);
        //添加文本内容
        TravelContent content = travel.getTravelContent();
        //关联主键
        content.setId(travel.getId());
        travelContentMapper.insert(content);
    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> listAll() {
        return travelMapper.selectAll();
    }

    @Override
    public void update(Travel travel) {
        travelMapper.updateByPrimaryKey(travel);
        //添加文本内容
        TravelContent content = travel.getTravelContent();
        //更新文本内容
        content.setId(travel.getId());
        travelContentMapper.updateByPrimaryKey(content);

    }


    @Override
    public PageInfo queryForList(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), true, false, true);
        qo.setOrderBy("t.lastUpdateTime DESC");
        List<Travel> list = travelMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void updateState(Travel travel) {
        if (travel.getState()==2){
            travel.setReleaseTime(new Date());
        }else {
            travel.setReleaseTime(null);
        }
        /**
         * 这个地方需要判断,然后发送不同的信息,发布消息,拒绝消息
         * 通过travel的id,获取到我们的travel对象.
         * 保存information以及他与用户之间的对应关系.
         */
        Travel travelEntry = travelMapper.selectByPrimaryKey(travel.getId());
        Information information = new Information();
        if (travel.getState().equals(Travel.STATE_RELEASE)){
            information.setType(Information.SYSTEM_MESSAGE_PUBLISH);
            information.setContent("恭喜您,你的文章"+travelEntry.getTitle()+"已经发布成功");
        }else {
            information.setType(Information.SYSTEM_MESSAGE_CANCEL_PUBLISH);
            information.setContent("你的文章"+travelEntry.getTitle()+"已经被拒绝发布");
        }
        information.setTitle(travelEntry.getTitle());
        User author = new User();
        author.setId(travel.getAuthor().getId());
        informationService.saveUserInformation(information, author);
        //更新文章的状态
        travelMapper.updateState(travel);
    }

    //全局搜索
    @Override
    public PageInfo globalSearch(SearchQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        qo.setOrderBy("t.releaseTime DESC");
        List<Travel> list = travelMapper.query(qo);
        return new PageInfo(list);
    }

    @Override
    public int globalNumberSearch(SearchQueryObject qo) {
        return travelMapper.selectForCount(qo);
    }

    @Override
    public void updateBrowseNum(Travel travel) {
        travelMapper.updateBrowseNum(travel);
    }

    @Override
    public int selectTravelsNums(Long userId) {
        return travelMapper.selectTravelsNums(userId);
    }

}
